package cn.uway.shiro;

import java.util.Collection;
import java.util.Set;

import org.apache.shiro.cache.CacheException;

import cn.uway.cache.LocalCache;


public class ShiroEhcache<K,V> implements ICache<K,V> {
	
    private LocalCache  ehcache;
    
    private String cacheName;
	
	
	public ShiroEhcache(LocalCache  ehcache, String cacheName) {
	     this.ehcache = ehcache;
	     this.cacheName = cacheName;
	}
	
	     
    @SuppressWarnings("unchecked")
	@Override  
    public V get(K key) throws CacheException {  
    	return (V)ehcache.getObject(this.cacheName, key.toString());
    }  
  
    @SuppressWarnings("unchecked")
    @Override  
    public V put(K key, V value) throws CacheException {  
        return (V)ehcache.putObject(this.cacheName, key.toString(), value);  
    }  
    
    @SuppressWarnings("unchecked")
    @Override
	public V put(K key, V value, int expireSeconds) throws CacheException {
    	 return (V)ehcache.putObjectWithExpireTime(this.cacheName, key.toString(), value, expireSeconds);  
	}  
  
  
    @SuppressWarnings("unchecked")
    @Override  
    public V remove(K key) throws CacheException {  
    	return (V)ehcache.removeObject(this.cacheName,key.toString()); 
    }  
    
  
    @Override  
    public int size() {  
        if (keys() == null)  {
        	 return 0;  
        }           
        return keys().size();  
    }  
  
    @SuppressWarnings("unchecked")
    @Override  
    public Set<K> keys() {         
        return  (Set<K>)ehcache.getKeys(this.cacheName);  
    }  
  
    @SuppressWarnings("unchecked")
    @Override  
    public Collection<V> values() {  
        return  (Collection<V>)ehcache.getValues(this.cacheName);  
    }  
    
    @Override  
    public void clear() throws CacheException {  
    	ehcache.clear(this.cacheName);
    }
   
}
